const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../services/user.js');
const app = getApp();

var interval;

Page({
  data: {
    imgUrl: app.globalData.imgurl,
    keyWord: '',
    hiddenName: true,
    adList: [],
    rexiao: [],
    text: "最新活动最新活动买哟送一机会难得快来选购！最新活动最新活动买哟送一机会难得快来选购！",
    marqueePace: 1,//滚动速度
    marqueeDistance: 0,//初始滚动距离
    marquee_margin: 30,
    size: 14,
    interval: 20, // 时间间隔
    newGoods: [],
    hotGoods: [],
    topics: [],
    brands: [],
    floorGoods: [],
    banner: [],
    channel: [],
    zhekou: [],
    pintuan: [],
    imgUrls: [
      'http://yanfademo.qd-weimob.com/hk-img/pic01.jpg',
      'http://yanfademo.qd-weimob.com/hk-img/pic01.jpg',
      'http://yanfademo.qd-weimob.com/hk-img/pic01.jpg'
    ],
    swiperIndex: 1,
    indicatorDots: false,
    autoplay: false,
    interval: 5000,
    duration: 1000,
    currentIndex: 0,//限时秒杀
    "firstList": ["LXT", "LXT"],
    "secondList": ["GFF", "GFF"],
    "thirdList": ["GFF", "GFF"],
    "fourList": ["GFF", "GFF", "GFF", "GFF"]
    
  },
  fenxiao: function () {
    var member = this.data.member;
    if (member.type == 0 && member.cardNo != null && member.cardNo != '') {

    } else {
      util.showSuccessToast('您已成为分销商');
    }
  },
  getMyselfData: function () {
    let that = this;
    util.request(api.MyselfUrl, {}, 'get').then(function (data) {
      if (data.type === 'success') {
        that.setData({
          nickname: data.data.member.nickname,
          avatarUrl: data.data.member.avatarUrl,
          cardNo: data.data.member.cardNo,
          member: data.data.member,
          orderCounts: data.data.orderCounts,
          couponCounts: data.data.couponCounts
        });
      } else {
        console.log(data.data.content);
      }
    });
  },
  getIndexData: function () {
    var page = wx.getStorageSync("page");
    var productStockId = wx.getStorageSync("productStockId");
    if (page && productStockId) {
      console.log("首页获取了productStockId跳转");
      wx.removeStorageSync("page");
      wx.removeStorageSync("productStockId");
    }
    if (page == "goods" && productStockId) {
      wx.navigateTo({
        url: '/pages/goods/goods?productStockId=' + productStockId
      })
    }
    if (page == "tuangou") {
      wx.navigateTo({
        url: '/pages/tuangou/tuangou?productStockId=' + productStockId
      })
    }
    let that = this;
    util.request(api.IndexUrl, {}, 'get').then(function (data) {
      if (data.type === 'success') {
        var lastSecond = data.data.lastSecond;
        if (lastSecond != null && lastSecond != '') {
          that.cutDown(lastSecond);
        }
        that.setData({
          adList: data.data.adList,
          rexiao: data.data.rexiao,
          zhekou: data.data.zhekou,
          zhekou2: data.data.zhekou2,
          pintuan: data.data.pintuan,
          shangxin: data.data.shangxin,
          forum: data.data.forum,
          videoList: data.data.videoList,
          topList: data.data.topList,
          zibu: data.data.zibu
        });
      } else {
        console.log(data.content);
      }
    });
  },
  swiperChange(e) {
    const that = this;
    that.setData({
      swiperIndex: e.detail.current,
    })
  },
  onLoad: function (options) {
    var page = options.page;
    if (page != null && page == 'cantuan') {
      var productStockId = options.productStockId;
      var assembleId = options.assembleId;
      var assembleItemId = options.assembleItemId;
      wx.navigateTo({
        url: '/pages/pintuan/pintuan?productStockId=' + productStockId + '&assembleId=' + assembleId + '&assembleItemId=' + assembleItemId
      })
    }
    if (page) {
      var recommendId = options.recommendId;
      var productStockId = options.productStockId;
      wx.setStorageSync("recommendId", recommendId);
      wx.setStorageSync("productStockId", productStockId);
      wx.setStorageSync("page", page);
    }
    var scene = options.scene;
    if (scene) {
      if (scene.indexOf("recommendId_") > -1) {
        var mid = scene.replace("recommendId_", "");
        wx.setStorageSync("recommendId", mid);
        console.log("通过个人中心小程序码生成的");
      }
      var parr = scene.split("_");
      if (parr[0] && parr[0] == "goods" && parr.length == 3) {
        console.log("通过分享商品生成的");
        var recommendId = parr[1];
        var productStockId = parr[2];
        wx.setStorageSync("recommendId", recommendId);
        wx.setStorageSync("page", "goods");
        wx.setStorageSync("productStockId", productStockId);
      }
    }
    /**
    console.log("分享页面recommendId:" + options.recommendId);
    var scene = options.scene;
    if (scene) {
      if (scene.indexOf("recommendId_") > -1) {
        var mid = scene.replace("recommendId_", "");
        wx.setStorageSync("recommendId", mid);
        console.log("index,扫码recommendId:" + mid);
      }
    }
    if(options.recommendId){
      wx.setStorageSync("recommendId", options.recommendId);
    }
    */
  },
  onShow: function (options) {
    clearInterval(interval);
    var isTest = app.globalData.isTest;
    if (isTest == 1) {
      this.getIndexData();
    } else {
      user.checkLogin(this);
    }
  },
  bindGetUserInfo: function (e) {
    user.loginByWeixin(e, this);
  },
  //倒计时
  cutDown: function (lastSecond) {
    var totalSecond = lastSecond;

    interval = setInterval(function () {
      // 秒数
      var second = totalSecond;

      // 天数位
      var day = Math.floor(second / 3600 / 24);
      var dayStr = day.toString();
      if (dayStr.length == 1) dayStr = '0' + dayStr;

      // 小时位
      var hr = Math.floor((second - day * 3600 * 24) / 3600);
      var hrStr = hr.toString();
      if (hrStr.length == 1) hrStr = '0' + hrStr;

      // 分钟位
      var min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
      var minStr = min.toString();
      if (minStr.length == 1) minStr = '0' + minStr;

      // 秒位
      var sec = second - day * 3600 * 24 - hr * 3600 - min * 60;
      var secStr = sec.toString();
      if (secStr.length == 1) secStr = '0' + secStr;

      this.setData({
        countDownDay: dayStr,
        countDownHour: hrStr,
        countDownMinute: minStr,
        countDownSecond: secStr,
      });
      totalSecond--;
      if (totalSecond < 0) {
        clearInterval(interval);
        this.setData({
          countDownDay: '00',
          countDownHour: '00',
          countDownMinute: '00',
          countDownSecond: '00',
          zhekou: []
        });
      }
    }.bind(this), 1000);
  },
  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function () {
    var that = this;
    var mid = wx.getStorageSync("memberId");
    if (!mid) {
      mid = "";
    }
    console.log("首页分享：" + mid);
    return {
      title: '月亮湾商城',
      path: 'pages/index2/index2?recommendId=' + mid
    }
  },
  //用户点击tab时调用
  titleClick: function (e) {
    let currentPageIndex =
      this.setData({
        //拿到当前索引并动态改变
        currentIndex: e.currentTarget.dataset.idx
      })
  },


})